﻿using System.Threading.Channels;
using MeterReader.WebApi.Models;
using MeterReader.WebApi.Services.Repositories;

namespace MeterReader.WebApi.Services
{
    public class DataService : IDataService
    {
        private readonly IRepository _repository;
        private Channel<IEnumerable<NewTMeter>> _channel = Channel.CreateUnbounded<IEnumerable<NewTMeter>>();

        public DataService(IRepository repository)
        {
            _repository = repository;
        }

        public async Task<List<string>> GetMeterList() => await _repository.GetMeterListAsync();

        public async Task SaveData()
        {
            await _channel.Reader.WaitToReadAsync();
            var newTMeters = await _channel.Reader.ReadAsync();
            // 写入数据库
            await _repository.SaveDataAsync(newTMeters);
        }

        public async Task<List<PointsInfo>> SearchHistory(string name, DateTime start, DateTime end) =>
            await _repository.SearchAsync(name, start, end);
    }
}
